package com.kinon.ogt.view.activities;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.Toast;

import com.kinon.ogt.R;
import com.kinon.ogt.utils.RxUtils;
import com.kinon.ogt.utils.SPrefsUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

/**
 * 佛祖保佑 永无BUG
 *
 * @author WangZhiYao
 * @date 2018/4/28
 */
public class SplashActivity extends AppCompatActivity {

    private static final int COUNT_DOWN_SECONDS = 3;
    private Disposable mDisposable;

    private String[] mPermissions = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_PHONE_STATE
    };

    private List<String> mPermissionList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        checkAppPermission();
    }

    private void checkAppPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            for (String mPermission : mPermissions) {
                if (ContextCompat.checkSelfPermission(this,
                        mPermission) != PackageManager.PERMISSION_GRANTED) {
                    mPermissionList.add(mPermission);
                }
            }
            if (mPermissionList.isEmpty()) {
                initView();
                return;
            }
            String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);
            ActivityCompat.requestPermissions(this, permissions, 0);
        } else {
            initView();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 0) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) {
                        Toast.makeText(this, "需要以上权限才能正常使用", Toast.LENGTH_SHORT).show();
                        checkAppPermission();
                        return;
                    }

                    initView();
                }
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void initView() {
        mDisposable = Flowable.interval(0L, 1L, TimeUnit.SECONDS)
                .compose(RxUtils.rxSchedulerHelper())
                .subscribe(aLong -> {
                    if (COUNT_DOWN_SECONDS - aLong <= 0) {
                        mDisposable.dispose();
                        if (SPrefsUtils.readFirstRun()) {
                            startActivity(new Intent(SplashActivity.this, IntroActivity.class));
                            SplashActivity.this.finish();
                        } else if (TextUtils.isEmpty(SPrefsUtils.readToken())) {
                            startActivity(new Intent(SplashActivity.this, SignInActivity.class));
                            SplashActivity.this.finish();
                        } else {
                            startActivity(new Intent(SplashActivity.this, MainActivity.class));
                            SplashActivity.this.finish();
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mDisposable) {
            mDisposable.dispose();
            mDisposable = null;
        }
    }
}
